﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Text;

namespace WuxiWTO.Web
{
    public partial class newComplain : System.Web.UI.Page
    {
        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user_name"] == null && Request.Cookies["wtouser"] == null)
            {
                Response.Redirect(Server.HtmlEncode("login.aspx?ret=newComplain.aspx"));
            }
            if (!IsPostBack)
            {
                InitStatusMsg();
                BindGrid();
            }
        }
        private void BindGrid()
        {
            var query = (from c in contex.Complaints orderby c.update_date descending where c.display_toppage == '1' select c).AsQueryable();

            List<Complaint> list = query.ToList();
            AspNetPager1.RecordCount = list.Count; ;
            psd.DataSource = list;
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptArticle.DataSource = psd;
            this.rptArticle.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptArticle_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                Complaint cat = item as Complaint;

                Label lblProcessTime = e.Item.FindControl("lblProcessTime") as Label;
                lblProcessTime.Text = cat.update_date.GetValueOrDefault().ToShortDateString();
            }
        }
        private void InitStatusMsg()
        {
            if (UserInfo.AllowRegValidate())
            {
                string uname = Session["user_name"].ToString();
                var user = contex.supporters.SingleOrDefault(p => p.supporter_login_name == uname);
                if (user.is_active != 'Y')
                {
                    this.btnInsert.Enabled = false;
                    lblErrMsg.Visible = true;
                }
                else
                {
                    this.btnInsert.Enabled = true;
                    lblErrMsg.Visible = false;
                }
            }
            else
            {
                this.btnInsert.Enabled = true;
                lblErrMsg.Visible = false;
            }
          
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["user_type"].ToString() == "I")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "typerr", "alert('个人用户不能进行投诉！');location='default.aspx';", true);
                this.btnInsert.Enabled = false;
                return;
            }
        }

        protected void btnInsert_Click(object sender,EventArgs e)
        {
            if (this.editor1.Text.Trim() == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "blankerr", "alert('请填写投诉的内容');", true);
                return;
            }
            else
            {
                Complaint com = new Complaint();
                com.question = this.editor1.Text.Trim();
                com.supporter_id = int.Parse(Session["user_id"].ToString());
                com.created_date = DateTime.Now;
                com.update_date = DateTime.Now;
                com.record_status = 'N';
                com.title = this.txtTitle.Text.Trim();
                contex.Complaints.InsertOnSubmit(com);
                contex.SubmitChanges();
                Response.Redirect("enterpriseComplain.aspx");
            }
        }
    }
}